package com.qf.admin.controller;

import com.qf.admin.entity.dto.Goods;
import com.qf.admin.http.Result;
import com.qf.admin.service.GoodsColorService;
import com.qf.admin.service.GoodsService;
import com.qf.admin.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Value("${upload.path}")
    private String path;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsColorService goodsColorService;

    @RequestMapping("list")
    public String toList(Model model){
        model.addAttribute("goodsList" , goodsService.getAvaliableGoods());
        return "goods/list";
    }

    @RequestMapping("add")
    public String toAdd(Model model , @RequestParam(value = "id" , required = false) Long goodsId){
        model.addAttribute("goods" , goodsService.getById(goodsId));
        model.addAttribute("goodsColorList" , goodsColorService.getByGoodsId(goodsId));
        model.addAttribute("goodsTypeList" , goodsTypeService.list());
        return "goods/add";
    }

    @RequestMapping("up")
    @ResponseBody
    public Result<?> up(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.up(goodsId);
        return Result.success();
    }

    @RequestMapping("down")
    @ResponseBody
    public Result<?> down(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.down(goodsId);
        return Result.success();
    }

    @RequestMapping("secKill")
    @ResponseBody
    public Result<?> secKill(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.secKill(goodsId);
        return Result.success();
    }


    @RequestMapping("upload")
    @ResponseBody
    public Result<?> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String filename = String.format("%s.%s" , sdf.format(new Date()) , suffix);
        file.transferTo(new File(String.format("%s%s" , path , filename)));
        return Result.success(String.format("http:localhost:8080/%s", filename));
    }

    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(Goods goods , String color , String pic , String stock){
        goodsService.saveGoods(goods , color , pic , stock);
        return Result.success();
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id" , required = true) Long goodsId){
        goodsService.del(goodsId);
        return Result.success();
    }




}
